//
//  RoomViewController.m
//  Model
//
//  Created by zeb-apple on 2017/5/26.
//  Copyright © 2017年 YDJH. All rights reserved.
//  室内房间

#import "RoomViewController.h"
#import "InsideWallView.h"
#import "HZFurniture_Header.h"
#import "HZRoom.h"
#import "HZRoomLogic.h"
#import "HZInsideWall.h"
#import "FurnitureActionView.h"
#import "HZFurniture.h"
#import "HZCommand.h"
#import "RoomSmallMap.h"
#import "HZOutsideRoomViewController.h"
#import "VentstacksScene.h"

@interface RoomViewController ()

@property (nonatomic, strong)   InsideWallView *insideWallView;
@property (nonatomic, strong)   VentstacksScene *ventstacksScene; //通风管道

@property (nonatomic, strong) HZRoom *room;

@end

@implementation RoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initView];
}
- (void)initView {
    
    [self.view addSubview:self.insideWallView];
    [self.view addSubview:self.actionView];
    [self.view addSubview:self.roomSmallMap];
}

- (void)setBuildingStructure:(NSMutableArray *)buildingStructure {
    _buildingStructure = buildingStructure;
    
}
#pragma mark
#pragma mark getter
- (VentstacksScene *)ventstacksScene {
    if (!_ventstacksScene) {
        __weak typeof(self) weakSelf = self;
        _ventstacksScene = [[VentstacksScene alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) callback:^(VentStacksDirection direction, HZRoom *room) {
            [weakSelf ventStacksActionWithDirection:direction room:room];
        }];
        _ventstacksScene.center = self.view.center;
        
    }
    return _ventstacksScene;
}
- (InsideWallView *)insideWallView {
    if (!_insideWallView) {
        __weak typeof(self) weakSelf = self;
        _insideWallView = [[InsideWallView alloc] initWithFrame:CGRectZero furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            [weakSelf.actionView reloadCollectionWithFurniture:furniture];
            [weakSelf.actionView show];
        } goOutBlock:^(InsideWallView *view) {
            //当前房间的位置
            NSIndexPath *indexPath = [HZRoomLogic goBackBuildingCurrentIndex];
            HZRoom *lastRoom = weakSelf.buildingStructure[indexPath.section][indexPath.row];
            //更新上个房间的状态
            lastRoom.roomState = RoomState_Came;
            weakSelf.dissmissBlock(self, Main_Wall,lastRoom);
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
        
    }
    return _insideWallView;
}
//指令集事件
- (void)actionWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    [super actionWithFurniture:furniture command:command];
    SEL selector = [HZRoomLogic goBackSelectFromRoomViewControllerActionWithFurniture:furniture command:command];
    if ([self respondsToSelector:selector]) {
        SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:furniture withObject:command]);
    }
    [self reloadWall:self.room.insidewall1];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.actionView dissmiss];
}



#pragma mark
#pragma mark action
// 切换房间
- (void)enterRoomWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    if (furniture.outside) { //室外门
        NSIndexPath *indexPath = furniture.indexPath; //当前房间的位置
        HZRoom *room = self.buildingStructure[indexPath.section][indexPath.row];
        self.dissmissBlock(self, Back_Wall,room);
        [self dismissViewControllerAnimated:YES completion:nil];
    }else { //室内门
        [self reloadWithRoom:[HZRoomLogic performSelectorWithFurniture:furniture command:command buildingStructure:self.buildingStructure]];
    }
}
//爬窗户
- (void)climbWindowWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    
    NSIndexPath *indexPath = furniture.indexPath; //当前房间的位置
    HZRoom *room = self.buildingStructure[indexPath.section][indexPath.row];
    self.dissmissBlock(self, Back_Wall,room);
    [self dismissViewControllerAnimated:YES completion:nil];
}
//进通风管道
- (void)enterVentstacksSceneWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    
    NSIndexPath *indexPath = furniture.indexPath;
    [self.ventstacksScene configureViewWithCurrentRoomIndex:indexPath andRoomArray:self.buildingStructure[indexPath.section]];
    [self.view addSubview:self.ventstacksScene];
}
//通风管道的实现
- (void)ventStacksActionWithDirection:(VentStacksDirection)direction room:(HZRoom *)room {
    
    switch (direction) {
        case VentStacksDirection_down:
            [self reloadWithRoom:room];
            [self.ventstacksScene removeFromSuperview];
            break;
        case VentStacksDirection_left:
            [self reloadWithRoom:room];
            break;
        case VentStacksDirection_right:
            [self reloadWithRoom:room];
            break;
        default:
            break;
    }
}

#pragma mark
#pragma mark reloadwall
//根据房间刷新数据
- (void)reloadWithRoom:(HZRoom *)room {
    
    if (room) {
        self.room = room;
        self.insideWallView.goOutImageView.hidden = !room.isFirstSurfaceRoom;
        //更新当前房间位置
        [HZRoomLogic updataBuildingCurrentIndex:room.position];
        
        HZInsideWall *wall = room.insidewall1;
        
        UIImage *image = [UIImage imageNamed:wall.wallPicture];
        
        self.insideWallView.image = image;
        self.insideWallView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        self.insideWallView.center = self.view.center;
        
        [self reloadWall:wall];
        room.roomState = RoomState_Coming;
        
        //更新小地图
        [self.roomSmallMap reloadData:(NSMutableArray *)[[self.buildingStructure reverseObjectEnumerator] allObjects]];
        
        //        [UIView transitionWithView: self.insideWallView
        //                          duration: 0.3f
        //                           options: UIViewAnimationOptionTransitionCrossDissolve
        //                        animations: ^(void)
        //         {
        //
        //         }
        //                        completion:^(BOOL finished) {
        //
        //
        //
        //                        }];
        
        
    }
    
}
- (void)reloadWall:(HZInsideWall *)wall {
    
    [self.insideWallView reloadWithInsideWall:wall];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
